<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Http\Controllers\Controller;
use Tymon\JWTAuth\Exceptions\JWTException;
use Auth;
use JWTAuth;
class LoginController extends Controller
{
    //
	use AuthenticatesUsers;
	public function __construct()
	{

//		$this->middleware('guest')->except('logout');
	}


	public function login(Request $request){
//
//		$credentials = $this->validate($request, [
//			'name' => 'required|string',
//			'password' => 'required|string',
//		]);
//		return $credentials;
		$credentials=['phone' => $request->name,
			'password' => $request->password];
//		return bcrypt('123456');
		try {
			if (! $token = JWTAuth::attempt($credentials)) {
				return response()->json([
					// 'status' => 'error',
					'error' => '认证失败，用户名或密码不正确'
				], 401);
			}
		} catch (JWTException $exception) {
			return response()->json([
				// 'status' => 'error',
				'error' => '未知错误，请稍后再试'
			], 500);
		}

		return response()->json([
			'code'=>200,
			'access_token' => $token,
			'token_type' => 'Bearer',
			'expires_in' => config('jwt.ttl') * 60
		]);
	}

	public function api_logout()
	{
		try {
			$this->guard()->logout();
		} catch (Exception $exception) {
			return response()->json([
				'status' => 'error',
				'message' => 'Unknown Error'
			], 500);
		}
		return response()->json([
			'status' => 'success',
			'message' => '注销成功'
		]);
	}



}
